package com.wyu.servlet;

import cn.hutool.db.Session;
import com.alibaba.druid.sql.visitor.functions.If;
import com.wyu.pojo.YG;
import com.wyu.service.YGService;
import com.wyu.service.YGServiceImpl;
import com.wyu.util.DesUtil;
import com.wyu.util.Md5Password;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ygh = request.getParameter("ygh");
        String mm = request.getParameter("mm");
        String zw = request.getParameter("zw");
        int type = Integer.parseInt(request.getParameter("type"));
        String remember = request.getParameter("remember");
        System.out.println("用户输入的工号: "+ygh+",密码: "+mm+"登陆人:"+type);

        HttpSession session = request.getSession();
        //获取用户输入的，及之前保存的session中的，比较两个是否一样
        //判断用户名和密码是否正确
        //用三层架构写项目，service，dao，web（controller）
        YGService ygService = new YGServiceImpl();
        //将ygh和加密后的密码交给service层做逻辑处理
        YG yg = ygService.login(ygh,mm,zw);

        System.out.println("复选框提交的值" +remember);

        if (yg!=null){
            String mm1 = yg.getMm();
            System.out.println("得到的密码是:"+mm1);
            //判断是否记住密码
            if (remember!=null){
                //创建两个新的cookie用来保存ygh和加密的密码
                Cookie cookie = new Cookie("ygh",ygh);
                cookie.setMaxAge(60*60*24);
                response.addCookie(cookie);
                Cookie cookie1 = new Cookie("mm",mm1);
                cookie1.setMaxAge(60*60*24);
                response.addCookie(cookie1);
                System.out.println("已记住密码");
            }else {
                //不记住密码,将name为密码的cookie删除
                Cookie cookie1 = new Cookie("mm",mm1);
                cookie1.setMaxAge(0);
                response.addCookie(cookie1);
                System.out.println("删除已经记住的密码");
            }
                switch (type) {
                    case 0:
                        if (yg.getZw().equals("后勤主管")){
                            session.setAttribute("yg", yg);
                            session.setAttribute("loginMsg", "");
                            response.sendRedirect("ZG.jsp");
                            return;
                        }
                        //登录失败
                        session.setAttribute("loginMsg","身份错误");
                        //重定向到登录界面
                        response.sendRedirect("index.jsp");
                        break;
                    case 1:
                        if (yg.getZw().equals("采购员")){
                            session.setAttribute("yg", yg);
                            session.setAttribute("loginMsg", "");
                            response.sendRedirect("CaiGou.jsp");
                            return;
                        }
                        //登录失败
                        session.setAttribute("loginMsg","身份错误");
                        //重定向到登录界面
                        response.sendRedirect("index.jsp");
                        break;
                    case 2:
                        if (yg.getBz().equals("2")){
                            if (yg.getZw().equals("仓库管理员")){
                                session.setAttribute("yg", yg);
                                session.setAttribute("loginMsg", "");
                                response.sendRedirect("CKGLY.jsp");
                                return;
                            }
                            session.setAttribute("yg", yg);
                            session.setAttribute("loginMsg", "");
                            response.sendRedirect("YG.jsp");
                            return;
                        }
                        //登录失败
                        session.setAttribute("loginMsg","身份错误");
                        //重定向到登录界面
                        response.sendRedirect("index.jsp");
                        break;
                    default:
                        break;
                }
            return;
        }
        //登录失败
        session.setAttribute("loginMsg","工号或者密码错误");
        //重定向到登录界面
        response.sendRedirect("index.jsp");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

}